package cn.skcks.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController
public class FileController {
	@PostMapping("/upload")
	public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

		// 获取文件名
		String fileName = file.getOriginalFilename();
		System.out.println("文件名 => " + fileName);

		if (fileName == null || fileName.equals("")) {
			return "error: 文件名不能为空";
		}

		String path = request.getServletContext().getRealPath("/upload");
		File realPath = new File(path);
		if (!realPath.exists()) {
			realPath.mkdir();
		}

		System.out.println("文件保存位置: " + realPath);

		InputStream inputStream = file.getInputStream();
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

		OutputStream outputStream = new FileOutputStream(new File(realPath, fileName));
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

		int len;
		byte[] buffer = new byte[1024];
		while ((len = bufferedInputStream.read(buffer)) != -1) {
			bufferedOutputStream.write(buffer, 0, len);
			bufferedOutputStream.flush();
		}

		inputStream.close();
		bufferedInputStream.close();
		outputStream.close();
		bufferedOutputStream.close();

		return "success";
	}


	@PostMapping("/upload2")
	public String upload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

		String path = request.getServletContext().getRealPath("/upload");
		File realPath = new File(path);
		if (!realPath.exists()) {
			realPath.mkdir();
		}

		System.out.println("文件保存位置: " + realPath);

		file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));

		return "success";
	}

	@RequestMapping("/download")
	public String download(HttpServletResponse response, HttpServletRequest request) throws Exception {
		String path = request.getServletContext().getRealPath("/upload");
		String fileName = "baidu.jpg";

		// 设置页面不缓存, 清空 buffer
		response.reset();
		// 设置字符编码
		response.setCharacterEncoding("UTF-8");
		// 设置 数据类型
		response.setContentType("multipart/form-data");
		// 设置 响应头
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));

		File file = new File(path, fileName);

		// 输入流 - 读取文件
		InputStream inputStream = new FileInputStream(file);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

		// 输出流 - 写入文件
		OutputStream outputStream = response.getOutputStream();
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

		byte[] buff = new byte[1024];
		int len;
		while ((len = bufferedInputStream.read(buff)) != -1) {
			bufferedOutputStream.write(buff, 0, len);
			bufferedOutputStream.flush();
		}

		bufferedOutputStream.close();
		outputStream.close();
		bufferedInputStream.close();
		inputStream.close();

		return "success";
	}
}